/**
 * CREATING CLONES.
 */
package  
{
	import flare.basic.*;
	import flare.core.*;
	import flare.loaders.collada.*;
	import flare.system.*;
	import flare.utils.*;
	import flash.display.*;
	import flash.events.*;
	
	[SWF(frameRate = 120, backgroundColor = 0x808080, width = 800, height = 600 )]
	/**
	 * ...
	 * @author Ariel Nehamd
	 */
	public class Test04 extends Sprite
	{
		private var scene:Scene3D;
		private var model:Pivot3D;
		
		public function Test04() 
		{
			ColladaLoader.texturesFolder = "textures";
			
			scene = new Scene3D( this );
			scene.camera.setPosition( 0, 0, -170 );
			
			model = scene.addChildFromFile( "elsa.dae" );
			
			scene.addEventListener( Scene3D.PROGRESS_EVENT, progressEvent );
			scene.addEventListener( Scene3D.COMPLETE_EVENT, completeEvent );
		}
		
		private function progressEvent(e:Event):void 
		{
			trace( scene.loadProgress );
		}
		
		private function completeEvent(e:Event):void 
		{
			// clone instances. you can not load a model or resource twice. you should clone them.
			var clone1:Pivot3D = model.clone();
			var clone2:Pivot3D = model.clone();
			
			clone1.x = -50;
			clone2.x = 50;
			
			scene.addChild( clone1 ); // you can also use "clone1.parent = scene;"
			scene.addChild( clone2 );
			
			// once we loaded the scene, we start to update.
			scene.addEventListener( Scene3D.UPDATE_EVENT, updateEvent );
		}
		
		private function updateEvent(e:Event):void 
		{
			// simple orbit camera.
			if ( Input3D.mouseDown )
			{
				scene.camera.rotateY( Input3D.mouseXSpeed, false, Vector3DUtils.ZERO );
				scene.camera.rotateX( Input3D.mouseYSpeed, true, Vector3DUtils.ZERO );
			}
			
			scene.camera.translateZ( scene.camera.getPosition().length * Input3D.delta / 20 );
		}
		
	}

}